var addClass;
require(['monitor'], function () {
	var selectCarId="" ;
    $('#monitor').monitor({
        canId: '11',
        callback: function (data) {
        	loadData(data);
        },
        clearData: function () {
        	loadData({});
        }
    });   
    
	//左侧树
	commonFun.leftTreeNodeClickObj['carChange'] = function(event,
			treeId, treeNode) {
		if($('#monitor').monitor('getStatus')=='monitor'){
			return;
		}
		selectCarId=treeNode.id;
	};
	
	addClass=function(num) {
		$("#btryBox tr").removeAttr("class");
		$("#boxRow" + num).attr("class", "active");
		var command = "{canId:'14',carId:'" + selectCarId + "',timeSpan:1,interval:0 ,boxNo:" + num + "}";
		$.fn.monitor.command(command,{callback:function(data){			
			loadSingleBatteryData(data);			
		}});	
	}
	
	//解析动力电池
	function loadData(data) {
		if (!$.isEmptyObject(data)) {
			$("#allowMoVolate").html(data.allowMoVolate);
			$("#allowMoCurrent").html(data.allowMoCurrent);
			$("#allowChargeTermVolate").html(data.allowChargeTermVolate);
			$("#allowTempt").html(data.allowTempt);
			$("#modularSeriesConNum").html(data.modularSeriesConNum);
			$("#modularParallConNum").html(data.modularParallConNum);
			$("#actualEnergy").html(data.actualEnergy);
			$("#ratedEnergy").html(data.ratedEnergy);			
			$("#totalVoltage").html(data.totalVoltage);
			$("#chargeDisCharCurrent").html(data.chargeDisCharCurrent);
			$("#soc").html(data.soc);
			$("#enduranceMile").html(data.enduranceMile);
			$("#highestBatteryVoltage").html(data.highestBatteryVoltage);
			$("#highestVoltageBoxNO").html(data.highestVoltageBoxNO);
			$("#highestVoltageInboxLocation").html(data.highestVoltageInboxLocation);			
			$("#lowestBatteryVoltage").html(data.lowestBatteryVoltage);
			$("#lowestVoltageBoxNO").html(data.lowestVoltageBoxNO);
			$("#lowestVoltageInboxLocation").html(data.lowestVoltageInboxLocation);			
			$("#highestTemperature").html(data.highestTemperature);
			$("#highestTemperatureBoxNO").html(data.highestTemperatureBoxNO);
			$("#hightestTemperatureInboxLocation").html(data.hightestTemperatureInboxLocation);			
			$("#highestBoxTept").html(data.highestBoxTept);					
			//生成电池箱体个数列表
			var boxNum = data.batteryBoxCount;
			createBoxTab(boxNum, data.batteryBoxLinkInfo);
		}
	}
	
	//生成电池箱体个数
	function createBoxTab(boxNum, batteryBoxLinkInfo) {
		$("#btryBox").html("");
		var tabStr = "";
		var boxAr = batteryBoxLinkInfo.split("");
		var z = 1;
		//if(boxNum>=1){
		tabStr = "<tr><th width='50%'>箱号</th><th width='50%'>连接状态</th></tr>";
		//}
		for (var i = boxNum - 1; i >= 0; i--) {
			var signStr = "<td class='green' id='boxStatus"+z+"'>正常</td>";
			if (boxAr[i] == '1') {
				signStr = "<td class='red' id='boxStatus"+z+"'>异常</td>";
			}
			tabStr += "<tr onclick='addClass(" + z + ")' id='boxRow" + z
					+ "'><td>第" + z + "箱</td>" + signStr + "</tr>";
			z++;
		}
		$("#btryBox").html(tabStr);
	}
	
	function loadSingleBatteryData(data) {
		//console.info("单体电池箱体解析");
		if (!$.isEmptyObject(data)) {
			$("#lowestModulVolate").html(data.lowestModulVolate);
			$("#highestModulVolate").html(data.highestModulVolate);
			$("#highestModulTempt").html(data.highestModulTempt);
			$("#modulTemptRange").html(data.modulTemptRange);
			
			//单体电池个数
			var voltages = data.batteryVoltageList || '';
			var voltageAr = voltages.split("/");

			var tmprt = data.temperatureList || '';
			var tmprtAr = tmprt.split("/");
			var voltageTab = "<tr><th width='17%' class='fw-b fs-14'>块号</th>"
					+ "<td width='17%' class='fw-b fs-14'>电压(v)</td>"
					+ "<th width='17%' class='fw-b fs-14'>块号</th>"
					+ "<td width='16%' class='fw-b fs-14'>电压(v)</td>"
					+ "<th width='17%' class='fw-b fs-14'>块号</th>"
					+ "<td width='16%' class='fw-b fs-14'>电压(v)</td></tr>";
			var _voltageTab = "";
			$("#voltageNum").html("单体电池个数：" + (voltageAr.length - 1) + "个");
			for (var i = 1; i < voltageAr.length; i++) {
				_voltageTab += "<th>第" + i + "块</th><td id='voltage_"+i+"'>"
						+ voltageAr[i - 1] + "</td>";
				if ((i % 3 == 0)) {
					_voltageTab = "<tr>" + _voltageTab + "</tr>";
					voltageTab += _voltageTab;
					_voltageTab = "";
				}
				else if(i == voltageAr.length-1){
					for(var j=0;j<3-(i%3);j++){
						_voltageTab += "<th></th><td></td>";
					}
					_voltageTab = "<tr>" + _voltageTab + "</tr>";
					voltageTab += _voltageTab;
				}
			}
			$("#voltageTab").html(voltageTab);
			//温度探头个数
			var tmprtTab = "<tr><th width='17%' class='fw-b fs-14'>探头位置</th>"
				+ "<td width='17%' class='fw-b fs-14'>温度(°C)</td>"
				+ "<th width='17%' class='fw-b fs-14'>探头位置</th>"
				+ "<td width='16%' class='fw-b fs-14'>温度(°C)</td>"
				+ "<th width='17%' class='fw-b fs-14'>探头位置</th>"
				+ "<td width='16%' class='fw-b fs-14'>温度(°C)</td></tr>";
			var tmpnum = 0;
			if (tmprtAr.length > 0) {
				tmpnum = tmprtAr.length - 1;
			}
			$("#tmprtNum").html("温度探头个数：" + tmpnum + "个");
			var _tmprtTab = "";
			for (var i = 1; i < tmprtAr.length; i++) {
				_tmprtTab += "<th>探头" + i
						+ "</th><td id='tmprAr_"+i+"'>" + tmprtAr[i - 1]
						+ "℃</td>";
				if (i % 3 == 0) {
					_tmprtTab = "<tr>" + _tmprtTab + "</tr>";
					tmprtTab += _tmprtTab;
					_tmprtTab = "";
				}else if(i == tmprtAr.length-1){
					for(var j=0;j<3-(i%3);j++){
						_tmprtTab += "<th></th><td></td>";
					}
					_tmprtTab = "<tr>" + _tmprtTab + "</tr>";
					tmprtTab += _tmprtTab;
				}
				$("#tmprtTab").html(tmprtTab);
			}			
		}
	}

});